package com.example.demo.utils;

import com.example.demo.entity.User;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegUtils {

    //mobile check
    public static boolean mobileCheck(String mobile){
        if(Objects.equals(mobile, ""))
            return true;
        if(mobile.length()!=11){
            System.out.println("length");
            return false;
        }
        String MOBILE_CHECK = "^1[34578]\\d{9}$";
        return mobile.matches(MOBILE_CHECK);
    }


    //email check
    public static boolean emailCheck(String email){
        if(Objects.equals(email, ""))
            return true;
        if(email.length()>95)
            return false;
        String EMAIL_CHECK = "^[A-Za-z0-9_\\-\\u4e00-\\u9fa5]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$";
        return email.matches(EMAIL_CHECK);
    }
    //username check
    public static boolean usernameCheck(String username){
        if(Objects.equals(username, ""))
            return false;
        if(username.length()>250)
            return false;
        String USERNAME_CHECK = "^(?:[\\u4e00-\\u9fa5]+)(?:·[\\u4e00-\\u9fa5]+)*$|^[a-zA-Z0-9]+\\s?[\\.·\\-()a-zA-Z]*[a-zA-Z]+$";
        return (username.length()<255&&username.matches(USERNAME_CHECK)&& !username.equals(""));

    }
    //pwd check
    public static boolean pwdCheck(String password){
        if(Objects.equals(password, ""))
            return false;
        String PW_CHECK = "^(?![\\d]+$)(?![a-zA-Z]+$)(?![-_]+$)[\\da-zA-Z_-]*.{6,32}";
        return password.matches(PW_CHECK);
    }
    //id check
    public static boolean idCheck(String id, String role){
        if(Objects.equals(id, "") || Objects.equals(role, ""))
            return false;
        String STUID_CHECK = "^[0-9]{6}$";
        String WORKID_CHECK = "^[0-9]{8}$";
        if(Objects.equals(role, "student"))
            return id.matches(STUID_CHECK);
        if(Objects.equals(role, "teacher"))
            return id.matches(WORKID_CHECK);
        return false;
    }
    //role check
    public static boolean roleCheck(String role){
        if(Objects.equals(role, ""))
            return false;
        return Objects.equals(role, "student") || Objects.equals(role, "teacher");
    }

    //school check
    public static boolean schoolCheck(String school){
        if(Objects.equals(school,""))
            return true;
        if(school.length()>50)
            return false;
        String SCHOOL_CHECK="^[\\u4E00-\\u9FA5A-Za-z0-9]+$";
        System.out.println(school);
        return school.matches(SCHOOL_CHECK);
    }

    //school check
    public static boolean majorCheck(String major){
        if(Objects.equals(major,""))
            return true;
        if(major.length()>50)
            return false;
        String MAJOR_CHECK="^[\\u4E00-\\u9FA5A-Za-z0-9]+$";
        return major.matches(MAJOR_CHECK);
    }

    //方法一：用JAVA自带的函数
    public static boolean isNumeric(String str){
        for (int i = str.length();--i>=0;){
            if (!Character.isDigit(str.charAt(i))){
                return false;
            }
        }
        return true;
    }

    //formatCheck(return error)
    public static String formatCheckString(HashMap<String,String> properties){

        String error="";
        if(!RegUtils.idCheck(properties.get("id"), properties.get("role"))){
            error+=("id非法或与角色不对应; ");
        }
        if(!RegUtils.usernameCheck(properties.get("name"))){
            error+=("姓名非法; ");
        }
        if(!IdcardUtils.isValid(properties.get("identityid"))){
            error+=("身份证非法; ");
        }
        if(!RegUtils.roleCheck(properties.get("role"))){
            error+=("角色非法; ");
        }
        if(!RegUtils.mobileCheck(properties.get("phonenumber"))){
            error+=("手机号非法; ");
        }
        if(!RegUtils.emailCheck(properties.get("mail"))){
            error+=("邮箱非法; ");
        }
        if(!RegUtils.schoolCheck(properties.get("uschool"))){
            error+=("学院非法; ");
        }
        if(!RegUtils.majorCheck(properties.get("umajor"))){
            error+=("专业非法; ");
        }
        return error;
    }

    //formatCheck
    public static HashMap formatCheck(String id, String name , String identityid, String phonenumber, String role, String school, String major,String mail){
        HashMap resMap = new HashMap();
        boolean isCorrect = true;
        //格式校验
        if(!RegUtils.usernameCheck(name)){
            resMap.put("name","illegal");
            isCorrect = false;
        }
        if(!IdcardUtils.isValid(identityid)){
            resMap.put("identityid","illegal");
            isCorrect = false;
        }
        if(!RegUtils.emailCheck(mail)){
            resMap.put("mail","illegal");
            isCorrect = false;
        }
        if(!RegUtils.mobileCheck(phonenumber)){
            resMap.put("phonenumber","illegal");
            isCorrect = false;
        }
        if(!RegUtils.idCheck(id,role)){
            resMap.put("role","unmatched");
            isCorrect = false;
        }
        if(!RegUtils.schoolCheck(school)){
            resMap.put("school","illegal");
            isCorrect = false;
        }
        if(!RegUtils.majorCheck(major)){
            resMap.put("major","illegal");
            isCorrect = false;
        }
        if(isCorrect)
            resMap.put("result","true");
        else
            resMap.put("result","false");

        return resMap;

    }

    //重复校验
    public static HashMap sameCheck(User user, String newrole, String newidentityid, String newmail, String newphonenumber, String newname, String newuschool, String newumajor){
        HashMap resMap = new HashMap();
        if(Objects.equals(user.getRole(), newrole)){
            resMap.put("roleWarning","identical");
        }
        if(Objects.equals(user.getIdentityId(), newidentityid)){
            resMap.put("identityidWarning","identical");
        }
        if(Objects.equals(user.getMail(), newmail)){
            resMap.put("mailWarning","identical");
        }
        if(Objects.equals(user.getPhoneNumber(), newphonenumber)){
            resMap.put("phonenumberWarning","identical");
        }
        if(Objects.equals(user.getUsername(), newname)){
            resMap.put("nameWarning","identical");
        }
        if(Objects.equals(user.getUschool(), newuschool)){
            resMap.put("schoolWarning","identical");
        }
        if(Objects.equals(user.getUmajor(), newumajor)){
            resMap.put("majorWarning","identical");
        }
        return resMap;
    }

    public static StringBuilder Numeric(List<String> value){
        int cnumCode =0;
        int cnameCode =1;
        int csnameCode=2;
        int cmnameCode=3;
        int chourCode=4;
        int ccreditCode=5;
        int ctimeCode=6;
        int croomCode=7;
        int capacityCode=8;
        int cintroCode=9;
        int tidCode=10;
        boolean isValid = true;
        StringBuilder error = new StringBuilder();
        if(!RegUtils.isNumeric(value.get(chourCode))){
            System.out.println(value.get(chourCode));
            error.append("学时格式不正确; ");
            isValid=false;
        }
        if(!RegUtils.isNumeric(value.get(ccreditCode))){
            System.out.println(value.get(ccreditCode));
            error.append("学分格式不正确; ");
            isValid=false;
        }
        if(!RegUtils.isNumeric(value.get(capacityCode))){
            System.out.println(value.get(capacityCode));
            error.append("容量格式不正确; ");
            isValid=false;
        }
        if(!isValid){
            //errorlist.add(error.toString());
            //continue;
            return error;
        }
        else
            return null;
    }


}

